package huaweiod.first;

import java.util.*;

/**
 * 华为OD机试真题 Java 实现【积木最远距离】【2022.11 Q4 新题】
 */
public class A_2023_19_BuildingBlocksMostDistinct {

    public static void main(String[] args) {
        //处理输入
        Scanner in = new Scanner(System.in);
        int count = in.nextInt();

        // process
        Map<Integer, List<Integer>> map = new HashMap<>();
        for (int i = 1; i <= count; i++) {
            int number = in.nextInt();
            if (map.containsKey(number)) {
                map.get(number).add(i);
            } else {
                List<Integer> list = new ArrayList<>();
                list.add(i);
                map.put(number, list);
            }
        }
        int ans = -1;
        for (List<Integer> list : map.values()) {
            if (list.size() > 1) {
                ans = Math.max(ans, list.get(list.size() - 1) - list.get(0));
            }
        }
        System.out.println(ans);
    }
}
